<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="4" labelWidth="90" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <!-- 问题信息 -->
    <div class="box">
      问题信息
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :height="'auto'"
        :isSelectColumn="false"
        :isToperInfo="false"
        columnsRef="ysjproblemTable"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <el-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
        </template>
      </FilterTable>
    </div>

    <!-- 答案信息 -->
    <div class="box">
      答案信息
      <FilterTable
        ref="answerTable"
        :columns="answerTableColums"
        :fetchapi="getQuestionApi"
        :params="fetchQuestionParams"
        :datakey="'records'"
        :height="'height:auto'"
        :isSelectColumn="false"
        :isToperInfo="false"
        :isColumnFilter="false"
        columnsRef="ysjProblemAnswerTable"
        :onSyncTableData="tableChangeFun"
        :onColumnsChange="columns => (this.answerTableColums = columns)"
      >
      </FilterTable>
    </div>

    <!-- 新增/编辑模板 -->
    <BaseDialog
      :visible.sync="visible"
      :title="(type == 'add' ? '新增' : '编辑') + '问题'"
      width="70%"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <problemPanel :type="type" :recordVersion="recordVersion" :questionId="questionId" @changeHandle="changeHandle" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { getQuestionnaireInfo, delQuestionnaireInfo, queryQuestionPagById } from '@/api/customerRepair/visitRules/questionnaireMake2';
import problemPanel from './problemPanel';

export default {
  name: 'problem',
  components: {
    problemPanel
  },
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.AnswerTable = null;
    return {
      topFilterList: this.createTopFilterList(), // 搜索表单列表
      visible: false,
      fetchApi: getQuestionnaireInfo, // 获取问题表格接口
      fetchParams: {}, // 获取问题表格参数
      columns: this.createTableColumns(), // 问题信息表格列
      getQuestionApi: queryQuestionPagById, // 获取问题表格接口
      fetchQuestionParams: {}, // 获取问题表格参数
      answerTableColums: this.createAnswerTableColums(), // 问题信息表格列
      type: '', // 新增or编辑
      recordVersion: '',
      questionId: '', // 编辑时问卷ID
      tableData: [] // 保存的问题信息表格数据
    };
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.AnswerTable = this.$refs.answerTable.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '问题名称',
          fieldName: 'questionName',
          placeholder: '问题名称'
        },
        {
          type: 'SELECT',
          label: '问题类型',
          fieldName: 'questionType',
          placeholder: '问题类型',
          itemList: this.createDictList('1571')
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'isValid',
          placeholder: '是否启用',
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '数据来源',
          initialValue: 10451001,
          disabled: true,
          itemList: this.createDictList('1045')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 180,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editProblem(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.getProblemById(props.row)}>
                  问题信息
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.deleteProblem(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '问题名称',
          dataIndex: 'questionName',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '问题内容',
          dataIndex: 'questionContent',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '问题类型',
          dataIndex: 'questionType',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.questionType, '1571')}</div>;
          }
        },
        {
          title: '是否启用',
          dataIndex: 'isValid',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.isValid, '1004')}</div>;
          }
        },
        {
          title: '是否必填',
          dataIndex: 'isMustFilled',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.isMustFilled, '1004')}</div>;
          }
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.dataSources, '1045')}</div>;
          }
        },
        {
          title: '使用次数',
          dataIndex: 'questionNumber',
          filterType: 'input',
          showOverflowTooltip: true
        }
      ];
    },
    createAnswerTableColums() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '答案名称',
          dataIndex: 'answerGroupName',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '答案说明',
          dataIndex: 'answerGroupDescript',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '答案选项',
          dataIndex: 'answerOption',
          filterType: 'input',
          showOverflowTooltip: true,
          render: props => {
            let res = '<span>';
            if (props.row.questionType === 15711001 || props.row.questionType === 15711002) {
              if (props.row.answerOption != '') {
                let answerOption = props.row.answerOption.split(',');
                let type = props.row.questionType === 15711001 ? 'radio' : 'checkbox';
                for (let i = 0; i < answerOption.length; i++) {
                  res += "<label><input name='Fruit' type='" + type + "' value='' />" + answerOption[i] + ' </label> ';
                }
              }
            }
            res += '</span>';
            return <p domPropsInnerHTML={res}></p>;
          }
        }
      ];
    },
    // 搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false, dmsFuncId: 404025, isTemplate: 10041001, dataSources: 10451001 });
      this.clearAnswerTable();
    },
    // 保存answerTable的数据，用于清空
    tableChangeFun(val) {
      this.tableData = val;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    clearAnswerTable() {
      this.$nextTick(() => {
        this.AnswerTable.EXECUTE_DELETE(this.tableData);
      });
    },
    closeHandler(val) {
      this.visible = val;
    },
    deleteProblem(row) {
      let data = {
        questionId: row.questionId
      };
      delQuestionnaireInfo(data).then(res => {
        if (res.resultCode === 200) {
          this.$message.success('删除成功');
          this.changeHandle({
            dataSources: 10451001 // 来源厂端
          });
        } else {
          this.$message.error('删除失败');
        }
      });
    },
    // 根据问卷ID查询其选项
    getProblemById(row) {
      let vat = {
        questionId: row.questionId
      };
      this.fetchQuestionParams = Object.assign({}, vat, { xhrAbort: false });
    },
    // 新增问卷
    add() {
      this.visible = true;
      this.type = 'add';
      this.questionId = '';
    },
    // 编辑问卷
    editProblem(row) {
      this.visible = true;
      this.type = 'edit';
      this.questionId = row.questionId + '';
      this.recordVersion = row.recordVersion;
    }
  }
};
</script>

<style lang="less" scoped>
.dialog-form {
  width: 100%;
  margin: 0;
}

.el-checkbox-group .el-checkbox + .el-checkbox {
  margin: 0;
}

.box {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  margin-top: 20px;
}
</style>
